home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 15 / develop 15 code / 3D Interface / Library / U3DDrawing.h < prev   
Encoding:
C/C++ Source or Header  |  1993-06-02  |  18.6 KB  |  510 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        U3DDrawing.h
  3.  
  4.     Contains:    Headers for 3D drawing routines.
  5.  
  6.     Written by:    Jamie Osborne, Robin Mair, Faulkner White, Henri Lamiraux
  7.  
  8.     Copyright:    © 1992-1993 by Apple Computer, Inc.
  9.  
  10. */
  11.  
  12. #ifndef __U3DDRAWING__
  13. #define __U3DDRAWING__
  14.  
  15. #ifndef __UCONTROL__
  16.     #include <UControl.h>
  17. #endif
  18.  
  19. #ifndef __ICONS__
  20.     #include "Icons.h"
  21. #endif
  22.  
  23. // 8Bit gray shade constants
  24. const     short     kRGB8BitGray1        =    61166;    //    Light gray
  25. const    short    kRGB8BitGray2        =    56797;    //    Slightly darker gray
  26. const    short    kRGB8BitGray3        =    52428;    //    Slightly darker gray
  27. const    short    kRGB8BitGray4        =    48059;    //    Slightly darker gray
  28. const    short    kRGB8BitGray5        =    43690;    //    Slightly darker gray
  29. const    short    kRGB8BitGray6        =    34952;    //    Slightly darker gray
  30. const    short    kRGB8BitGray7        =    30583;    //    Slightly darker gray
  31. const    short    kRGB8BitGray8        =    21845;    //    Slightly darker gray
  32. const    short    kRGB8BitGray9        =    17476;    //    Slightly darker gray
  33. const    short    kRGB8BitGray10        =    8738;    //    Slightly darker gray
  34. const    short    kRGB8BitGray11        =    4369;    //    Dark gray
  35.  
  36. // 4Bit gray shade constants
  37. const     short    kRGB4BitGray1        =    49152;    //    Light gray
  38. const    short    kRGB4BitGray2        =    32768;    //    Medium gray
  39. const    short    kRGB4BitGray3        =    8192;    //    Dark gray
  40.  
  41. // Some constants for drawing grays
  42. const CRGBColor kLightGray = CRGBColor(kRGB8BitGray1, kRGB8BitGray1, kRGB8BitGray1);
  43. const CRGBColor kLightGray2 = CRGBColor(kRGB8BitGray2, kRGB8BitGray2, kRGB8BitGray2);
  44. const CRGBColor kLightGray4 = CRGBColor(kRGB8BitGray4, kRGB8BitGray4, kRGB8BitGray4);
  45. const CRGBColor kMediumLightGray = CRGBColor(kRGB8BitGray5, kRGB8BitGray5, kRGB8BitGray5);
  46. const CRGBColor kMediumGray = CRGBColor(kRGB8BitGray6, kRGB8BitGray6, kRGB8BitGray6);
  47.  
  48. //======================================================================================
  49. // InitU3DDrawing
  50. //    This function initializes the 3D library.  You must call this function in your 
  51. //        initialization routine.
  52. //======================================================================================
  53.  
  54. pascal void InitU3DDrawing();
  55.  
  56.  
  57. //==================================================================================== 
  58. //    •••••••••••••••••••••••••••• 3D Utility Classes •••••••••••••••••••••••••••••••••••
  59. //==================================================================================== 
  60.  
  61. //====================================================================================== 
  62. //    CGRAPHICSSTATE
  63. //     A stack-based object that remembers the graphics state (BW and color)
  64. //        and restores it when it passes out of scope
  65. //====================================================================================== 
  66.     
  67. class CGraphicsState
  68. {
  69. private:
  70.     CRGBColor        fSaveForeColor;
  71.     CRGBColor        fSaveBackColor;
  72.     PenState        fSavePenState;
  73.     TextStyle        fSaveTextStyle;
  74.  
  75. public:
  76.             CGraphicsState();            // Constructor is called automatically
  77.             ~CGraphicsState();            // Destructor called automatically
  78.         
  79.     void     Save();
  80.     void    Restore();
  81.         
  82. };    // CGraphicsState
  83.  
  84.  
  85. //======================================================================================
  86. // CDRAWPERDEVICE
  87. //    A stack-based object that allows drawing in different pixel depths across
  88. //        different monitors
  89. //    If you're not using C++, just use DeviceLoop.  It's faster than CDrawPerDevice
  90. //    However, if you are using C++, CDrawPerDevice can save you some headaches
  91. //    having to deal with static drawing routines and the like
  92. //======================================================================================
  93.  
  94. class CDrawPerDevice 
  95. {
  96. private:
  97.     CRect            fGlobalArea;
  98.     GDHandle        fGDHandle;
  99.     RgnHandle        fSaveClip;
  100.     FocusRec        fFocus;
  101.     Boolean            fDoneOldQD;
  102.         
  103. public:
  104.                     CDrawPerDevice();
  105.                     CDrawPerDevice(const CRect& area);
  106.                     ~CDrawPerDevice();
  107.         
  108.     pascal void     SetDrawingArea(const CRect& area);
  109.     pascal Boolean    NextDevice(short& pixelSize);
  110.         
  111. };    // CDrawPerDevice
  112.  
  113.  
  114. //======================================================================================
  115. // CPenNormal
  116. //    Sets the fore and back color to black and white and calls PenNormal
  117. //======================================================================================
  118.  
  119. pascal void     CPenNormal();
  120.  
  121.  
  122. //==================================================================================== 
  123. //    •••••••••••••••••••••••••••• 3D TView Adorners •••••••••••••••••••••••••••••••••••
  124. //==================================================================================== 
  125.  
  126. const IDType kGrayBackgroundAdorner = 'grba';
  127. const IDType kWhiteBackgroundAdorner = 'whba';
  128. const IDType k3DGrayBackgroundAdorner = '3dgb';
  129. const IDType k3DLineTopAdorner = '3dlt';
  130. const IDType k3DLineBottomAdorner = '3dlb';
  131. const IDType k3DLineLeftAdorner = '3dll';
  132. const IDType k3DLineRightAdorner = '3dlr';
  133. const IDType k3DFrameAdorner = '3dfr';
  134.  
  135.  
  136. class TWhiteBackgroundAdorner : public TAdorner
  137. {    
  138. public:
  139.     virtual pascal void        Initialize();
  140.     virtual pascal void        IWhiteBackgroundAdorner(Boolean freeOnDeletion);
  141.     virtual pascal void        Draw(TView* itsView, const VRect& area);
  142. };
  143.  
  144. class TGrayBackgroundAdorner : public TAdorner
  145. {    
  146. public:
  147.     virtual pascal void        Initialize();
  148.     virtual pascal void        IGrayBackgroundAdorner(Boolean freeOnDeletion);
  149.     virtual pascal void        Draw(TView* itsView, const VRect& area);
  150. };
  151.  
  152.  
  153. class T3DGrayBackgroundAdorner : public TGrayBackgroundAdorner
  154. {
  155. public:
  156.     virtual pascal void        Initialize();
  157.     virtual pascal void        I3DGrayBackgroundAdorner(Boolean freeOnDeletion);
  158.     virtual pascal void        Draw(TView* itsView, const VRect& area);
  159. };
  160.  
  161.  
  162. class T3DLineTopAdorner : public TAdorner
  163. {
  164. public:
  165.     virtual pascal void        Initialize();
  166.     virtual pascal void        I3DLineTopAdorner(Boolean freeOnDeletion);
  167.     virtual pascal void        Draw(TView* itsView, const VRect& area);
  168. };
  169.  
  170.  
  171. class T3DLineBottomAdorner : public TAdorner
  172. {
  173. public:
  174.     virtual pascal void        Initialize();
  175.     virtual pascal void        I3DLineBottomAdorner(Boolean freeOnDeletion);
  176.     virtual pascal void        Draw(TView* itsView, const VRect& area);
  177. };
  178.  
  179. class T3DLineLeftAdorner : public TAdorner
  180. {
  181. public:
  182.     virtual pascal void        Initialize();
  183.     virtual pascal void        I3DLineLeftAdorner(Boolean freeOnDeletion);
  184.     virtual pascal void        Draw(TView* itsView, const VRect& area);
  185. };
  186.  
  187. class T3DLineRightAdorner : public TAdorner
  188. {
  189. public:
  190.     virtual pascal void        Initialize();
  191.     virtual pascal void        I3DLineRightAdorner(Boolean freeOnDeletion);
  192.     virtual pascal void        Draw(TView* itsView, const VRect& area);
  193. };
  194.  
  195.  
  196. class T3DFrameAdorner : public TAdorner
  197. {
  198. public:
  199.     virtual pascal void        Initialize();
  200.     virtual pascal void        I3DFrameAdorner(Boolean freeOnDeletion);
  201.     virtual pascal void        Draw(TView* itsView, const VRect& area);
  202. };
  203.  
  204.  
  205. //==================================================================================== 
  206. //    ••••••••••••••••• TControl classes and auxiliary adorners •••••••••••••••••••••••
  207. //==================================================================================== 
  208.  
  209. //==================================================================================== 
  210. //    T3DCheckBox
  211. //    A 3D version of the check box control.  This class circumvents the control CDEF
  212. //==================================================================================== 
  213.  
  214. class T3DCheckBox : public TCheckBox
  215. {
  216. protected:
  217.     VRect            fDrawBox;
  218.     CRGBColor        fForeColor, fBackColor;
  219.     
  220. public:    
  221.     virtual pascal void        Initialize();
  222.     virtual pascal void     DoPostCreate(TDocument *itsDocument);
  223.     virtual pascal void     I3DCheckBox (TView* itsSuperView, const VPoint& itsLocation,
  224.                                  const VPoint& itsSize, SizeDeterminer itsHSizeDet,
  225.                                  SizeDeterminer itsVSizeDet, const CStr255& itsLabel,
  226.                                  Boolean isTurnedOn);
  227.     virtual pascal TObject* Clone();
  228.     virtual pascal void     Free();
  229.  
  230.     virtual pascal void     DoMouseCommand(VPoint& theMouse,TToolboxEvent*,CPoint);                    
  231.     virtual pascal void     TrackMouse(TrackPhase aTrackPhase, VPoint& anchorPoint, VPoint& previousPoint,
  232.                                          VPoint& nextPoint, Boolean);                    
  233.  
  234.     virtual pascal void        Draw(const VRect& area);
  235.     virtual pascal void     DimState(Boolean state,Boolean redraw);
  236.     virtual pascal void        SetLongVal(VCoordinate itsVal, Boolean redraw);
  237.     virtual pascal void     HiliteState(Boolean state,Boolean redraw);
  238.     virtual pascal void     Hilite();    
  239.     virtual pascal void     Dim();    
  240.     inline pascal void        SetBackColor(CRGBColor    newColor) {fBackColor = newColor;}
  241.     virtual pascal void     InstallColor(const CRGBColor& theColor, Boolean redraw);
  242.  
  243. private:
  244.     virtual pascal void     DrawBoxText(const CStr255& s, const CRect& box, Boolean preferOutline);
  245.     virtual pascal void        DrawBox();
  246.     virtual pascal void        DrawCheck();
  247.     
  248. };  // T3DCheckBox
  249.  
  250.  
  251. //==================================================================================== 
  252. //    T3DRadio
  253. //    A 3D version of the radio button.  This class circumvents the control CDEF
  254. //==================================================================================== 
  255.  
  256. class T3DRadio : public TRadio
  257. {
  258. protected:
  259.     VRect            fDrawBox;
  260.     CRGBColor        fForeColor, fBackColor;
  261.     
  262. public:    
  263.     virtual pascal void        Initialize();
  264.     virtual pascal void     DoPostCreate(TDocument *itsDocument);
  265.     virtual pascal void     I3DRadio (TView* itsSuperView, const VPoint& itsLocation,
  266.                                      const VPoint& itsSize, SizeDeterminer itsHSizeDet,
  267.                                     SizeDeterminer itsVSizeDet, const CStr255& itsLabel,
  268.                                      Boolean isTurnedOn);
  269.     virtual pascal TObject* Clone();
  270.     virtual pascal void     Free();
  271.  
  272.     virtual pascal void     DoMouseCommand(VPoint& theMouse,TToolboxEvent*,CPoint);                    
  273.     virtual pascal void     TrackMouse(TrackPhase aTrackPhase, VPoint& anchorPoint, VPoint& previousPoint,
  274.                                          VPoint& nextPoint, Boolean);                    
  275.  
  276.     virtual pascal void        Draw(const VRect& area);
  277.     virtual pascal void     DimState(Boolean state,Boolean redraw);
  278.     virtual pascal void        SetLongVal(VCoordinate itsVal, Boolean redraw);
  279.     virtual pascal void     HiliteState(Boolean state,Boolean redraw);
  280.     virtual pascal void     Hilite();    
  281.     virtual pascal void     Dim();    
  282.     inline pascal void        SetBackColor(CRGBColor    newColor) {fBackColor = newColor;}
  283.     virtual pascal void     InstallColor(const CRGBColor& theColor, Boolean redraw);
  284.  
  285. private:
  286.     virtual pascal void     DrawBoxText(const CStr255& s, const CRect& box, Boolean preferOutline);
  287.     virtual pascal void        DrawBox();
  288.     virtual pascal void        DrawCheck();
  289. };
  290.  
  291.  
  292. //==================================================================================== 
  293. //    T3DButton
  294. //    A 3D version of the push button.  This class circumvents the control CDEF
  295. //==================================================================================== 
  296.  
  297. class T3DButton : public TButton 
  298. {
  299. protected:
  300.     TAdorner*        f3DAdorner;
  301.     CRGBColor        fHilitedTextColor;
  302.     
  303. public:
  304.     virtual pascal void Initialize();
  305.     virtual pascal void DoPostCreate(TDocument *itsDocument);
  306.     virtual pascal void I3DButton(TView* itsSuperView, const VPoint& itsLocation,
  307.                                         const VPoint& itsSize, SizeDeterminer itsHSizeDet,
  308.                                         SizeDeterminer itsVSizeDet, const CStr255& itsLabel);
  309.     virtual pascal TObject* Clone();
  310.     virtual pascal void Free();
  311.     virtual pascal void CreateButtonAdorner();
  312.     
  313.     virtual pascal void DoMouseCommand(VPoint& theMouse,TToolboxEvent*,CPoint);                                        
  314.     virtual pascal void TrackMouse(TrackPhase aTrackPhase, VPoint& anchorPoint, VPoint& previousPoint,
  315.                                          VPoint& nextPoint, Boolean);                    
  316.  
  317.     virtual pascal void DimState(Boolean state,Boolean redraw);
  318.     virtual pascal void HiliteState(Boolean state,Boolean redraw);
  319.     virtual pascal void Draw(const VRect& area);
  320.     virtual pascal void Hilite();    
  321.     virtual pascal void Dim();    
  322.     inline  pascal void SetHilitedTextColor(const CRGBColor& color) { fHilitedTextColor = color; }
  323.  
  324. private:
  325.     virtual pascal void DrawBoxText(const CStr255& s, const CRect& box, Boolean preferOutline );
  326.                                                                         
  327. };    // T3DButton
  328.  
  329.  
  330. //==================================================================================== 
  331. //    T3DTextButtonAdorner
  332. //   Adorner used by the T3DButton class for 3D drawing.
  333. //==================================================================================== 
  334.     
  335. //    Round Rect Oval Constants
  336. const short                 kOvalWidth            =    10;
  337. const short             kOvalHeight            =    10;
  338.  
  339. const IDType             k3DTextButtonAdorner= '3dtb';
  340.  
  341. class T3DTextButtonAdorner : public TAdorner 
  342. {
  343. public:
  344.     virtual pascal void        I3DTextButtonAdorner(Boolean freeOnDeletion);
  345.     virtual pascal void        Draw(TView* itsView, const VRect& area);
  346.  
  347. private:
  348.     virtual pascal void        Draw8Bit(const CRect& rect, Boolean hilite );
  349.     virtual pascal void        Draw4Bit(const CRect& rect, Boolean hilite );
  350.     virtual pascal void        Draw1Bit(const CRect& rect, Boolean hilite );
  351.     virtual pascal void        Frame(const CRect& rect);
  352.     virtual pascal void        TopLeftCorner(const CRect& rect, const CRGBColor light,
  353.                                             const CRGBColor dark, Boolean hilite );
  354.     virtual pascal void        BotLeftCorner(const CRect& rect, const CRGBColor light,
  355.                                             const CRGBColor dark, Boolean hilite );
  356.     virtual pascal void        TopRightCorner(const CRect& rect, const CRGBColor light,
  357.                                             const CRGBColor dark, Boolean hilite );
  358.     virtual pascal void        BotRightCorner(const CRect& rect, const CRGBColor light,
  359.                                             const CRGBColor dark, Boolean hilite );
  360.  
  361. };    //    T3DTextButtonAdorner
  362.  
  363.  
  364. //==================================================================================== 
  365. //    ••••••••••••••••• 3DIconButton class and auxiliary adorner •••••••••••••••••••••••
  366. //==================================================================================== 
  367.  
  368. //==================================================================================== 
  369. //    TYPES
  370. //==================================================================================== 
  371.  
  372. typedef    short ButtonMode;            //    Used to store the mode for Icon Buttons
  373.     
  374. //==================================================================================== 
  375. //    CONSTANTS
  376. //==================================================================================== 
  377.  
  378. const     IDType             k3DIconAdorner             =     '3dia';        //    IDType for our adorner
  379. const    Boolean            kButtonOn                =    TRUE;        // State button on
  380. const    Boolean            kButtonOff                =    FALSE;        // State button off
  381. const    ButtonMode        kButtonMode                =    1;            // Normal button Mode for state buttons
  382. const    ButtonMode        kSwitchMode                =    2;            //    Toggle Mode for state buttons - default
  383. const    ButtonMode        kRadioMode                =    3;            // Exclusivity Mode for state buttons
  384.  
  385. //==================================================================================== 
  386. //    T3DIconAdorner
  387. //   Adorner used by the T3DIconButton class for 3D drawing.
  388. //==================================================================================== 
  389.  
  390. class T3DIconAdorner : public TAdorner 
  391. {
  392. public:
  393.     virtual pascal void        I3DIconAdorner(Boolean freeOnDeletion);
  394.     virtual pascal void        Draw(TView* itsView, const VRect& area);
  395.  
  396. private:
  397.     virtual pascal void        Draw8Bit(const CRect& rect, Boolean hilite);
  398.     virtual pascal void        Draw4Bit(const CRect& rect, Boolean hilite);
  399.     virtual pascal void        Draw1Bit(const CRect& rect,    Boolean hilite);
  400.     virtual pascal void        Frame(const CRect& rect);
  401.     virtual pascal void        TopLeftSide(const CRect& rect);
  402.     virtual pascal void        BotRightSide(const CRect& rect);
  403.     virtual pascal void        TopLeftCorner(const CRect& rect, const CRGBColor light,
  404.                                             const CRGBColor dark );
  405.     virtual pascal void        BotLeftCorner(const CRect& rect, const CRGBColor light,
  406.                                             const CRGBColor dark );
  407.     virtual pascal void        TopRightCorner(const CRect& rect, const CRGBColor light,
  408.                                             const CRGBColor dark );
  409.     virtual pascal void        BotRightCorner(const CRect& rect, const CRGBColor light,
  410.                                             const CRGBColor dark, Boolean hilite );
  411.  
  412. };    //    T3DIconAdorner
  413.  
  414.  
  415. //==================================================================================== 
  416. //    TIconSuite
  417. //        Base class for the T3DIconButton.  This class draws icon suites (icl#).
  418. //==================================================================================== 
  419.  
  420. const EventNumber     mIconSuiteHit            =    1100;        // Hit in an Icon Suite
  421.     
  422. class     TIconSuite:    public    TControl 
  423. {
  424. private:
  425.  
  426.     ResNumber                 fIconSuiteRsrcID;        // Resource ID for the family of icons
  427.     IconAlignmentType        fAlignment;                //    Type of alignment we would like
  428.     Handle                     fDataHandle;            //    Data handle for the icon family
  429.     IconSelectorValue        fSelectorValue;
  430.  
  431. public:    
  432.     virtual pascal void     Initialize ();        
  433.     virtual pascal void     IIconSuite (TView* itsSuperView, const VPoint& itsLocation,
  434.                                     const VPoint& itsSize, SizeDeterminer itsHSizeDet,
  435.                                     SizeDeterminer itsVSizeDet, ResNumber itsRsrcID,
  436.                                     IconAlignmentType    alignment, IconSelectorValue selectorValue);
  437.     virtual pascal void     DoPostCreate(TDocument *itsDocument);
  438.     
  439.     virtual pascal TObject* Clone();
  440.     virtual pascal void     Free();    
  441.     virtual pascal void     ReleaseIconSuite();
  442.     
  443.     virtual pascal void     GetIconRect(VRect& theRect);
  444.     virtual pascal void     SetIconSuiteRsrcID(short itsRsrcID, IconSelectorValue selectorValue, 
  445.                                                 Boolean redraw );
  446.     virtual pascal void     SetIconSuite(Handle theSuite, Boolean redraw );
  447.     virtual pascal void     SetAlignment(IconAlignmentType newAlignment, Boolean redraw );
  448.  
  449.     virtual pascal void     Draw(const VRect& area);
  450.     virtual pascal void     Dim();    
  451.     virtual pascal void     Hilite();
  452.         
  453. private:
  454.     virtual pascal void     DoPlotIconSuite(IconTransformType transform);
  455.     
  456. };    // TIconSuite
  457.  
  458.  
  459. //==================================================================================== 
  460. //    T3DIconButton
  461. //        Implementation of 3d Icon button in color where available.
  462. //==================================================================================== 
  463. const EventNumber     mIconButtonHit            =    1102;        // Hit in an Icon Suite
  464.     
  465.  
  466. class T3DIconButton : public TIconSuite
  467. {
  468. protected:
  469.     TAdorner*    f3DIconAdorner;            //    Reference to the adorner that does 
  470.     ButtonMode    fMode;                    // What mode is this button operating under the initial
  471.                                         // possibilities are :     kRadioMode 
  472.                                         //                        kSwitchMode
  473.                                         //                        kButtonMode
  474.     Boolean        fState;                    //    Used to maintain the state of the button during tracking
  475.         
  476. private:
  477.     short        fIconSize;                // Either 12, 16 or 32
  478.  
  479. public:
  480.     
  481.                         T3DIconButton() {};                    
  482.     virtual pascal void Initialize();
  483.     virtual pascal void I3DIconButton(TView* itsSuperView, const VPoint& itsLocation,
  484.                                         const VPoint& itsSize, SizeDeterminer itsHSizeDet,
  485.                                         SizeDeterminer itsVSizeDet, short iconSize,
  486.                                         ResNumber itsRsrcID, ButtonMode mode,
  487.                                         Boolean state );
  488.     virtual pascal void DoPostCreate(TDocument *itsDocument);        
  489.     virtual pascal void CreateButtonAdorner ();
  490.     
  491.     virtual pascal void GetIconRect(VRect& theRect);
  492.     virtual pascal void SetIconRsrcID(short itsRsrcID, Boolean redraw );
  493.     virtual pascal void SetMode(ButtonMode newMode);
  494.     virtual pascal ButtonMode GetMode();
  495.     virtual pascal Boolean IsSelected();
  496.     virtual pascal void TrackMouse(TrackPhase aTrackPhase,VPoint& , 
  497.                                     VPoint& , VPoint& nextPoint,Boolean );            
  498.     virtual pascal void Hilite();                             
  499.     virtual pascal void Dim();                                 
  500.  
  501. private:
  502.     virtual pascal short GetIconSize(const VPoint& viewSize);
  503.     virtual pascal void SetIconSuiteRsrcID(short itsRsrcID, IconSelectorValue selectorValue, 
  504.                                             Boolean redraw );
  505.     virtual pascal void SetIconSuite(Handle theSuite, Boolean redraw);
  506.                                     
  507. };    // T3DIconButton
  508.  
  509.  
  510. #endif __U3DDRAWING__